//
//  UIColor+Gray.m
//  CqlivingCloud
//
//  Created by xhl_dabaoMac on 2021/12/14.
//  Copyright © 2021 xinhualong. All rights reserved.
//

#import "UIColor+Gray.h"
#import <objc/runtime.h>

@implementation UIColor (Gray)

+ (void)load{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        method_exchangeImplementations(class_getClassMethod(self, @selector(colorWithRed:green:blue:alpha:)), class_getClassMethod(self, @selector(c_colorWithRed:green:blue:alpha:)));
    });
}


+ (UIColor *)c_colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha{
    CGFloat gray = red * 0.299 +green * 0.587 + blue * 0.114;
    UIColor *grayColor = [UIColor colorWithWhite:gray alpha:alpha];
    return  grayColor;
}


- (UIColor *)changeGray{
    CGColorRef cgColor = self.CGColor;
    NSInteger num = CGColorGetNumberOfComponents(cgColor);
    if (num == 4) {
        const CGFloat *comp = CGColorGetComponents(cgColor);
        CGFloat red = comp[0];
        CGFloat green = comp[1];
        CGFloat blue = comp[2];
        CGFloat alpha = comp[3];
        UIColor *grayColor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
        return grayColor;
    }
    return self;
}
@end
